package character;

import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Node;
import com.jme3.scene.control.CameraControl;
import com.jme3.scene.control.CameraControl.ControlDirection;
import mygame.DataStore;
import mygame.DefaultValues;
import mygame.MyCharacterControl;

public abstract class CharacterCreator {

    public static AbstractCharacter createCharacter(Vector3f location) {
	//load character settings
	CharacterSettings charSettings = new CharacterSettings();
	charSettings.setHeight(DefaultValues.CHARACTER_SETTING_HEIGHT);
	charSettings.setRadius(DefaultValues.CHARACTER_SETTING_RADIUS);
	charSettings.setMass(DefaultValues.CHARACTER_SETTING_MASS);
	charSettings.setStepHeight(DefaultValues.CHARACTER_SETTING_STEP_HEIGHT);
	charSettings.setWalkSpeed(DefaultValues.CHARACTER_SETTING_WALK_SPEED);
        charSettings.setRunSpeed(DefaultValues.CHARACTER_SETTING_RUN_SPEED);
	charSettings.setBuildSpeed(DefaultValues.CHARACTER_SETTING_BUILD_SPEED);
	charSettings.setHarvestSpeed(DefaultValues.CHARACTER_SETTING_HARVEST_SPEED);

	//create character control
	//TODO refigure out appropriate radius
	//for some reason, the height of the capsule is bigger than you tell it to be. scaling by this value makes it what you expect
        CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(charSettings.getRadius(), charSettings.getHeight()*0.5767f);
	MyCharacterControl characterControl = new MyCharacterControl(capsuleShape, charSettings.getStepHeight());
	characterControl.setMaxSlope(DefaultValues.CHARACTER_SETTING_MAX_SLOPE);
	characterControl.setGravity(DefaultValues.CHARACTER_GRAVITY);
	characterControl.setJumpSpeed(DefaultValues.CHARACTER_JUMP_SPEED);
	characterControl.setFallSpeed(DefaultValues.CHARACTER_FALL_SPEED);

	//create character node
	Node charModel = (Node)DataStore.assetManager.loadModel("Models/Oto/Oto.mesh.xml");
        HumanoidCharacter character = new HumanoidCharacter(charModel, charSettings, characterControl);
        character.setName("Oto");
	character.scale(0.18f); //this should be relative to height?
	characterControl.setPhysicsLocation(location);
	
	//add camera control
	//TODO this can probably be removed..? i see no use for cams on characters
	Camera enemyCam = DataStore.player.getCamera().clone();
	character.addControl(new CameraControl(enemyCam, ControlDirection.SpatialToCamera));

	//add ai control
	CharacterAIControl aiControl = new CharacterAIControl();
	character.addControl(aiControl);

        return character;
    }
}
